package com.centaline.androidsalesblog.rx.request;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.sale.RegionPostsApi;
import com.centaline.androidsalesblog.model.businissobject.RegionPostBo;
import com.centaline.androidsalesblog.model.responseobject.RegionPostRo;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscriber;

/**
 * 租售小区房源数量
 * Created by guilin on 16/2/16.
 */
final class RequestRegionPostsOnSubscribe implements Observable.OnSubscribe<ArrayList<RegionPostBo>> {

    final Class<?> tag;

    RequestRegionPostsOnSubscribe(Class<?> tag) {
        this.tag = tag;
    }

    @Override
    public void call(final Subscriber<? super ArrayList<RegionPostBo>> subscriber) {
        new RegionPostsApi(tag, RegionPostRo.class, new IResponseListener<RegionPostRo>() {
            @Override
            public void onResponse(RegionPostRo regionPostRo) {
                if (!subscriber.isUnsubscribed() &&
                        regionPostRo != null &&
                        regionPostRo.getList() != null) {
                    subscriber.onNext(regionPostRo.getList());
                }
                subscriber.onCompleted();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                subscriber.onCompleted();
            }
        }).startRequest();
    }

}
